<?php
/**
 * 购物车
 */
namespace V3\Controller;

use Think\Controller;

class CartController extends ApiCommonController
{

    //添加商品到购物车
    public function add()
    {
        $data['product_id'] = trim(I('post.product_id'));
        if (!in_array('',$data)){
            $gdata = M('Goods')->where('id=' . $data['product_id'])->find();		
            //dump($gdata);die();
            if ($gdata) {
                $data['user_id'] = trim(I('post.user_id'));
                $data['product_brand'] = $gdata['brand'];
                $data['product_name'] = $gdata['name'];
                $data['product_title'] = $gdata['title'];
                $data['product_desc'] = $gdata['base_info'];
                $data['product_img'] = $gdata['picture'];
                $data['product_price'] = $gdata['price'];
                $data['product_count'] = trim(I('post.product_count'));
                $data['product_color'] = trim(I('post.product_color'));
                $data['product_guige'] = trim(I('post.product_guige'));
                $data['jxs_id'] = $gdata['movie_id'];              
                $name = M('Movie_v3')->getFieldById($data['jxs_id'],'name');
                if($name!=''){
                	$data['jxs_title'] = $name ;
                }else {
                    $return_data['status'] = '2';
                    $return_data['info'] = '失败!';
                }
				
        
               
                $data['created'] = time();
                $data['modified'] = time();
                $re = M('Cart')->add($data);
                if ($re) {
                    $return_data['status'] = '1';
                    $return_data['info'] = '成功!';
                } else {
                    $return_data['status'] = '2';
                    $return_data['info'] = '失败!';
                }
            } else {
                $return_data['status'] = '2';
                $return_data['info'] = '失败!';
            }

        } else {
            $return_data['status'] = '2';
            $return_data['info'] = '失败!';
        }

        $this->ajaxReturn($return_data);

    }

    //移除购物车商品
    public function del(){
       	$id = trim(I('id'));
		if(empty($id)){
			$this->ajaxReturn(array(
				'status' => '2',
				'info'   => '缺少必要参数',
			));
		}
		$re = M('Cart')->where('id='.$id)->delete();   
		if ($re) {
	 		$return_data['status'] = '1';
			$return_data['info'] = '成功!';
		}else {
	 		$return_data['status'] = '2';
			$return_data['info'] = '失败!';
		}  
        $this->ajaxReturn($return_data);
    }


    //修改购物车内商品数量
    public function editnum(){
		//Start  By Nicky ====================================================
		$id = trim(I('id'));
		$alg = trim(I('alg'));
		 
		if(empty($id)||empty($alg)){
			$this->ajaxReturn(array(
				'status' => '2',
				'info'   => '缺少必要参数',
			));
		}
		$cart = M('Cart')->where('id='.$id)->select();
		if(empty($cart)){
			$this->ajaxReturn(array(
				'status' => '2',
				'info'   => '参数提交有误',
			));
		}
		if($alg=='add'){  //增加购买数量
			$product = M('Goods')->where('id='.$cart['product_id'])->select();
			//当购买数量大于库存数量时
			if($cart['product_count']+1>$product['number']){
				$return_data = array(
					'status'=>'2',
					'info' => '超出商品库存，最多能购买'.$product['number'].'件',
					'data' => $product['number']
				);
			}else{
				//增加购买数量
				$rs = M('Cart')->where(array('id'=>$id))->setInc('product_count');
				if($rs){
					$return_data = array(
						'status'=>'1',
						'info' => '更新完成',
						'data' => $product['number']+1
					);
				}else{
					$return_data = array(
						'status'=>'2',
						'info' => '更新失败，请重试',
						'data' => $product['number']
					);
				}
			}
		}elseif($alg=='sub'){  //减少购买数量
			//购买数量不能小于1件
			if($cart['product_count']==1){
				$return_data = array(
					'status'=>'2',
					'info' => '购买数量不能小于1件',
					'data' => $product['number']
				);
			}
			//减少购买数量
			$rs = M('Cart')->where(array('id'=>$id))->setDec('product_count');
				if($rs){
					$return_data = array(
						'status'=>'1',
						'info' => '更新完成',
						'data' => $product['number']+1
					);
				}else{
					$return_data = array(
						'status'=>'2',
						'info' => '更新失败，请重试',
						'data' => $product['number']
					);
				}
		}else{
			$this->ajaxReturn(array(
				'status' => '2',
				'info'   => '参数提交有误',
			));
		}
		$this->ajaxReturn($return_data);
		//End  By Nicky ====================================================
		
/*	
       	$id = trim(I('post.id'));
        $data['user_id'] = trim(I('post.user_id'));		
		$user_id = M('Cart')->getFieldById($id,'user_id');
				
		if ($data['user_id'] == $user_id) {
			        
            $gre = M('Goods')->where('id='.$id)->select();
			$alg = trim(I('post.alg'));
			   
				if ($alg==1) {
           		 		$gre['number'];
						
        			}else {
            			$return_data = '2';
            			$return_data['info'] = '失败!';
        			}       
            
        } else {
            $return_data = '2';
            $return_data['info'] = '失败!';
        }
        
		if($alg==2){
			$id= trim(I('post.id'));			
			$user_id = M('Cart')->getFieldById($id,'user_id');
			$uid= trim(I('post.user_id'));
			
			if($uid == $user_id){
								
			$product_id = M('Cart')->getFieldById($id,'product_id');
			$number=M('Goods')->where('id='.$product_id)->select();	
			$count=$number['number'];
			$cnum = trim(I('post.product_count'));
			$ncoun= $cnum - 1;	
			
				if($count>=$ncoun){
					
					$data['product_count']=$ncoun;
					$re=M('Cart')->save($data);
					
					if($re){
						
					$return_data['status'] = '1';
	                $return_data['info'] = '成功!';
	                $return_data['data'] = $ncoun;
					
					}else{
						
					$return_data['status'] = '2';
	                $return_data['info'] = '失败!';
	                $return_data['data'] = $cnum ;	
					
				}			
			}else {
            			$return_data = '2';
            			$return_data['info'] = '参数错误！';
        			}
  			      }
   				 }
 			$this->ajaxReturn($return_data); 
*/

			
			}
    //获取购物车
       public function getcart() 
    {

        $user_id = trim(I('get.user_id'));
        if ($user_id) {
            $where['user_id'] = $user_id;
            $cartData = M('Cart')->where($where)->select();
            if ($cartData) {
                $return_data['status'] = '1';
                $return_data['info'] = '成功!';
                $return_data['data'] = $cartData;
            } else {
                $return_data['status'] = '1';
                $return_data['info'] = '结果为空!';
                $return_data['data'] = array();
            }
        } else {
            $return_data['status'] = '2';
            $return_data['info'] = '参数不足!';
        }
        $this->ajaxReturn($return_data);

	}


}
